﻿Imports System.Web
Imports System.Web.Services
Imports System.Reflection
Public Class hndAnnuncio
    Implements System.Web.IHttpHandler
    Public photoPath As String = ConfigurationManager.AppSettings("photoPath").Replace("~", String.Empty)
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim op As String = context.Request("op").Trim 'METODO DA RICHIAMARE
            Dim Params As String() = (From r As String In context.Request.Form.Keys Where r <> "op" Order By r _
                          Select context.Request.Form(r).Trim).ToArray
            If Params.Count = 0 Then
                Params = (From r As String In context.Request.QueryString.Keys Where r <> "op" And r <> "_" _
                          And r <> "callback" Order By r Ascending Select context.Request.QueryString(r).Trim).ToArray
            End If
            Dim t As Type = Me.GetType
            Dim m As MethodInfo = t.GetMethod(op)
            Dim result As String = String.Empty
            If m IsNot Nothing Then
                result = m.Invoke(Me, Params)
            End If
            context.Response.Write(result)
        Catch ex As Exception
            context.Response.Write(String.Empty)
        End Try
    End Sub
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    Public Function DeleteImage(ByVal IDAnnuncio As String, ByVal IDImmagine As String) As String
        Try
            Dim img As AnnuncioImmagini = DAL.Context.AnnuncioImmagini.Where(Function(i) i.IDAnnuncio = New Guid(IDAnnuncio) And i.IDImmagine = CInt(IDImmagine)).Single
            If img IsNot Nothing Then
                DAL.Context.AnnuncioImmagini.DeleteOnSubmit(img)
                DAL.Context.SubmitChanges()
                Return "1"
            Else
                Return "0"
            End If
        Catch ex As Exception
            Return "0"
        End Try
    End Function
    Public Function UpdateImage(ByVal Descrizione As String, ByVal IDAnnuncio As String, ByVal IDImmagine As String) As String
        Try
            Dim img As AnnuncioImmagini = DAL.Context.AnnuncioImmagini.Where(Function(i) i.IDAnnuncio = New Guid(IDAnnuncio) And i.IDImmagine = CInt(IDImmagine)).Single
            If img IsNot Nothing Then
                img.Descrizione = Descrizione
                DAL.Context.SubmitChanges()
                Return "1"
            Else
                Return "0"
            End If
        Catch ex As Exception
            Return "0"
        End Try
    End Function
End Class